//
//  LocationTool.m
//  Tourism
//
//  Created by 鲁万玺 on 2017/7/6.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "LocationTool.h"
#import <CoreLocation/CoreLocation.h>   

static LocationTool *locationTool;
static dispatch_once_t onceToken;

@interface LocationTool()<CLLocationManagerDelegate>

@property (nonatomic, strong)CLLocationManager *locationManager;
@property (nonatomic, strong)CLGeocoder * geocoder;

@property (nonatomic,  copy)locationBlock block;
@property (nonatomic, assign)BOOL isBlock;

@end

@implementation LocationTool

//获取当前用户
+ (instancetype)shareIntance
{
    if(locationTool == nil)
    {
        dispatch_once(&onceToken, ^{
            locationTool = [[LocationTool alloc] init];
        });
    }
    return locationTool;
}

- (instancetype)init
{
    if (self = [super init])
    {
        [self initializeLocationService];
    }
    return self;
}

- (void)initializeLocationService
{
    _locationManager = [[CLLocationManager alloc] init];
    [_locationManager requestWhenInUseAuthorization];
    //[_locationManager requestAlwaysAuthorization];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    _geocoder = [[CLGeocoder alloc] init];
}

- (void)startLocation:(locationBlock)block
{
    self.block = block;
    self.isBlock = NO;
    [_locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *newLocation = locations[0];
    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
    NSLog(@"旧的经度：%f,旧的纬度：%f",oldCoordinate.longitude,oldCoordinate.latitude);
    [manager stopUpdatingLocation];
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];

    CLLocation * location = locations.lastObject;
    CLLocationDegrees latitude = location.coordinate.latitude;
    CLLocationDegrees longitude = location.coordinate.longitude;

    [LocationTool shareIntance].latitude = latitude;
    [LocationTool shareIntance].longitude = longitude;

    WS(ws)
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError * _Nullable error) {
        for (CLPlacemark *place in placemarks) {
            NSLog(@"name,%@",[place.addressDictionary objectForKey:@"Name"]);                      // 位置名
            NSLog(@"thoroughfare,%@",[place.addressDictionary objectForKey:@"Thoroughfare"]);      // 街道
            NSLog(@"subThoroughfare,%@", [place.addressDictionary objectForKey:@"SubThoroughfare"]);// 子街道
            NSLog(@"locality,%@", [place.addressDictionary objectForKey:@"Locality"]);              // 市
            NSLog(@"subLocality,%@", [place.addressDictionary objectForKey:@"SubLocality"]);        // 区
            NSLog(@"country,%@", [place.addressDictionary objectForKey:@"Country"]);                // 国家

            NSString * area = [place.addressDictionary objectForKey:@"SubLocality"];//地区
            if (!ws.isBlock)
            {
                ws.isBlock = YES;
                ws.block(area);
                [manager stopUpdatingLocation];
            }
        }
    }];
}
//- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//    [manager stopUpdatingLocation];
//    CLLocation * location = locations[0];
//    CLLocationDegrees latitude = location.coordinate.latitude;
//    CLLocationDegrees longitude = location.coordinate.longitude;
//
//    [LocationTool shareIntance].latitude = latitude;
//    [LocationTool shareIntance].longitude = longitude;
//
//    WS(ws)
//    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//        if (placemarks.count > 0) {
//            CLPlacemark *placemark = [placemarks objectAtIndex:0];
//            NSLog(@"%@",placemark.name);
//            //获取城市
//            NSString *distirct = placemark.subLocality;
//            if (!distirct)
//            {
//                distirct = placemark.administrativeArea;
//            }
//            ws.address = distirct;
//            @synchronized(ws)
//            {
//                if (!ws.isBlock)
//                {
//                    ws.isBlock = YES;
//                    ws.block(distirct);
//                    [manager stopUpdatingLocation];
//                }
//            }
//        }
//        else if (error == nil && [placemarks count] == 0)
//        {
////            NSLog(@"No results were returned.");
//        }
//        else if (error != nil)
//        {
////            NSLog(@"An error occurred = %@", error);
//        }
//    }];
//
//}

@end
